package com.linxz.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.linxz.ui.GuiHelper;
import com.linxz.ui.R;
import com.wang.avi.AVLoadingIndicatorView;


/**
 * @author Linxz
 * 创建日期：2019年10月07日 18:59
 * version：v4.5.4
 * 描述：关注好友按钮
 */
public class FollowView extends FrameLayout implements View.OnClickListener{

    private RelativeLayout layoutBg;
    private TextView tvLabel;
    private AVLoadingIndicatorView loadView;
    private boolean follow;
    private OnFollowChangeListener mOnFollowChangeListener;
    private boolean isLoading;

    public void setOnFollowChangeListener(OnFollowChangeListener onFollowChangeListener){
        this.mOnFollowChangeListener=onFollowChangeListener;
    }

    public FollowView(@NonNull Context context) {
        super(context);
    }

    public FollowView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public FollowView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array=context.obtainStyledAttributes(R.styleable.FollowView);
        follow=array.getBoolean(R.styleable.FollowView_follow,false);
        array.recycle();
        init();
        setListener();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.user_view_follow,this);
        layoutBg=findViewById(R.id.layoutBg);
        tvLabel=findViewById(R.id.tvLabel);
        loadView=findViewById(R.id.loadView);
        if (follow){
            layoutBg.setBackgroundResource(R.drawable.ui_circle_yellow);
            tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
            tvLabel.setText("加关注");
            GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend);
        }else{
            layoutBg.setBackgroundResource(R.drawable.edt_circle_gray);
            tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.tv_hint));
            tvLabel.setText("已关注");
            GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend2);
        }
    }

    public void setListener(){
        setOnClickListener(this);
    }

    public void setFollow(boolean follow){
        this.follow=follow;
        if (follow){
            layoutBg.setBackgroundResource(R.drawable.ui_circle_yellow);
            tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
            tvLabel.setText("加关注");
            GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend);
        }else{
            layoutBg.setBackgroundResource(R.drawable.edt_circle_gray);
            tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.tv_hint));
            tvLabel.setText("已关注");
            GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend2);
        }
        hideLoading();
    }

    /**
     * 0:关注 1:不关注 2:互相关注
     * */
    public void setFollowType(int followType){
        switch (followType){
            case 0:
                follow=true;
                layoutBg.setBackgroundResource(R.drawable.edt_circle_gray);
                tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.tv_hint));
                tvLabel.setText("已关注");
                GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend2);
                break;
            case 1:
                follow=false;
                layoutBg.setBackgroundResource(R.drawable.ui_circle_yellow);
                tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.white));
                tvLabel.setText("加关注");
                GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend);
                break;
            case 2:
                this.follow=true;
                layoutBg.setBackgroundResource(R.drawable.edt_circle_gray);
                tvLabel.setTextColor(ContextCompat.getColor(getContext(), R.color.tv_hint));
                GuiHelper.setDrawableLft(tvLabel, R.drawable.user_icon_add_friend2);
                tvLabel.setText("互相关注");
                break;
                default:
                    break;
        }
        hideLoading();
    }

    public void showLoading(){
        if (follow){
            loadView.setIndicatorColor(ContextCompat.getColor(getContext(), R.color.tv_gray));
        }else{
            loadView.setIndicatorColor(ContextCompat.getColor(getContext(), R.color.white));
        }
        loadView.show();
        loadView.setVisibility(View.VISIBLE);
        tvLabel.setVisibility(View.GONE);
        isLoading=true;
    }

    public void hideLoading(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                loadView.hide();
                loadView.setVisibility(View.GONE);
                tvLabel.setVisibility(View.VISIBLE);
                isLoading=false;
            }
        }, 1000);
    }



    @Override
    public void onClick(View v) {
        if (mOnFollowChangeListener!=null && !isLoading){
            mOnFollowChangeListener.onChange(!follow);
        }
    }

    public interface OnFollowChangeListener{
        void onChange(boolean follow);
    }
}
